﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WebClass
{
    public class ViewQueryItem
    {
        public Exp Ex { get; set; }
        public List<QueryControlData> ListQueryControlData { get; set; }
        public ViewQueryItem()
        {
            Ex = new Exp();
            ListQueryControlData = new List<QueryControlData>();
        }

    }
    public class Exp
    {
        public Guid ID { get; set; }
        public string Name { get; set; }

    }
    public class ViewQueryOperation
    {
        ResearchDBDataContext researchDB = new ResearchDBDataContext();
        public List<ViewQueryItem> GetViewQuery(int taskID,int newsIndex,out News _news,out bool isLast,out int count)
        {
            _news = new News();
            List<ViewQueryItem> listViewQuery = new List<ViewQueryItem>();
            List<int> listNewsID = new List<int>();
            var newsResult = from e in researchDB.tb_experiments
                             where e.taskID == taskID
                             group e by e.newsID into g
                             select g;

            foreach(var news in newsResult)
            {
                listNewsID.Add(news.Key.Value);
            }
            count = listNewsID.Count;
            if (listNewsID.Count > 0)
            {
                if (newsIndex == listNewsID.Count - 1)
                {
                    isLast = true;
                }
                else
                {
                    isLast = false;
                }
                if (newsIndex >= listNewsID.Count)
                {
                    return listViewQuery;
                }
                int currentNewsID = listNewsID.ElementAt(newsIndex);
                int tmpNewsID = currentNewsID;

                var newsResult3 = from n in researchDB.tb_news
                                 where n.ID == tmpNewsID
                                 select n;
                foreach (var item in newsResult3)
                {
                    _news.ID = item.ID;
                    _news.Title = item.title;
                    _news.Content = item.newsContent;
                }
                var expResult = from e in researchDB.tb_experiments
                                where e.taskID == taskID && e.newsID == tmpNewsID
                                group e by e.expID into g
                                select g;
                foreach (var exp in expResult)
                {
                    ViewQueryItem v = new ViewQueryItem();
                    v.Ex.ID = Guid.Parse(exp.Key);
                    bool isFirst = true;
                    foreach (var item in exp)
                    {
                        if (isFirst == true)
                        {
                            v.Ex.Name = item.tb_exp.name;
                            isFirst = false;
                        }
                        foreach (var query in item.tb_experimentsQueries)
                        {
                            QueryControlData queryControlData =  new QueryControlData();
                            queryControlData.Query = query.query;
                            queryControlData.NewsID = tmpNewsID;
                            if (query.position != null)
                            {
                                queryControlData.Position = (int)query.position;
                            }
                            else
                            {
                                queryControlData.Position = -2;
                            }
                            v.ListQueryControlData.Add(queryControlData);
                        }
                    }
                    listViewQuery.Add(v);
                }
            }
            else
            {
                isLast = true;
            }
            return listViewQuery;
        }
    }
}
